﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

public enum MenuEntryDirection
{
    HORIZONTAL,
    VERTICAL,
    STARTGAME,
    CHARSELECTION,
    OPTIONSCREEN,
    PAUSESCREEN,
    NONE
}

abstract class MenuScreen : GameScreen
{
    MenuEntryDirection direction;
    public MenuEntryDirection Direction
    {
        get { return direction; }
        set { direction = value; }
    }

    List<MenuEntry> menuEntries = new List<MenuEntry>();
    protected IList<MenuEntry> MenuEntries
    {
        get { return menuEntries; }
    }

    protected string okSound, selectedSound, music = @"Audio\Musica Menus";
    
    protected int selectedEntry = 0;
    protected MenuEntry SelectedMenuEntry
    {
        get
        {
            if ((selectedEntry < 0) || (selectedEntry >= menuEntries.Count))
            {
                return null;
            }
            return menuEntries[selectedEntry];
        }
    }

    public MenuScreen()
    {
        TransitionOnTime = TimeSpan.FromSeconds(0.5);
        TransitionOffTime = TimeSpan.FromSeconds(0.5);


        okSound = @"Audio\s01";
        selectedSound = @"Audio\s02";
/*
        okSound = @"Audio\m_ok";
        selectedSound = @"Audio\m_join";
*/

        AudioManager.PlayMusic();
    }

    public override void HandleInput()
    {
        int oldSelectedEntry = selectedEntry;

        if (direction == MenuEntryDirection.NONE || direction == MenuEntryDirection.VERTICAL)
        {
            // Move to the previous menu entry?
            if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
            {
                selectedEntry--;
                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count - 1;
                while(!menuEntries[selectedEntry].IsSelectable)
                {
                    selectedEntry--;
                    if (selectedEntry < 0)
                        selectedEntry = menuEntries.Count - 1;
                }
            }

            // Move to the next menu entry?
            if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
            {
                selectedEntry++;
                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0; 
                while (!menuEntries[selectedEntry].IsSelectable)
                {
                    selectedEntry++;
                    if (selectedEntry >= menuEntries.Count)
                        selectedEntry = 0;
                }
            }
        }

        if (direction == MenuEntryDirection.NONE || direction == MenuEntryDirection.HORIZONTAL)
        {
            // Move to the previous menu entry?
            if (InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
            {
                selectedEntry--;
                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count - 1;
                while (!menuEntries[selectedEntry].IsSelectable)
                {
                    selectedEntry--;
                    if (selectedEntry < 0)
                        selectedEntry = menuEntries.Count - 1;
                }
            }

            // Move to the next menu entry?
            if (InputManager.IsActionTriggered(InputManager.Action.CursorRight))
            {
                selectedEntry++;
                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0;
                while (!menuEntries[selectedEntry].IsSelectable)
                {
                    selectedEntry++;
                    if (selectedEntry >= menuEntries.Count)
                        selectedEntry = 0;
                }
            }
        }

        if (direction == MenuEntryDirection.STARTGAME)
        {
            if (InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
            {
                /*if (selectedEntry == 2) selectedEntry = 0;
                else
                {
                    selectedEntry--;
                    if (selectedEntry < 0)
                        selectedEntry = 1;
                }*/
                
            }

            // Move to the next menu entry?
            if (InputManager.IsActionTriggered(InputManager.Action.CursorRight))
            {
                /*if (selectedEntry == 2) selectedEntry = 1;
                else
                {
                    selectedEntry++;
                    if (selectedEntry >= menuEntries.Count - 1)
                        selectedEntry = 0;
                }*/
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
            {
                /*if (selectedEntry == 0) selectedEntry = 2;
                else if (selectedEntry == 1) selectedEntry = 2;
                     else if (selectedEntry == 2) selectedEntry = 0;*/
                if (selectedEntry == 0) selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 0;
            }

            // Move to the next menu entry?
            if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
            {
                /*if (selectedEntry == 0) selectedEntry = 2;
                else if (selectedEntry == 1) selectedEntry = 2;
                     else if (selectedEntry == 2) selectedEntry = 0;*/
                if (selectedEntry == 0) selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 0;
            }
        }

        if (direction == MenuEntryDirection.CHARSELECTION)
        {
            if (InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
            {
                if (selectedEntry == 0)  selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 0;
                else if (selectedEntry == 2) selectedEntry = 3;
                else if (selectedEntry == 3) selectedEntry = 2;
                else if (selectedEntry == 4) selectedEntry = 4;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorRight))
            {
                if (selectedEntry == 0) selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 0;
                else if (selectedEntry == 2) selectedEntry = 3;
                else if (selectedEntry == 3) selectedEntry = 2;
                else if (selectedEntry == 4) selectedEntry = 4;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
            {
                if (selectedEntry == 0) selectedEntry = 4;
                else if (selectedEntry == 1) selectedEntry = 4;
                else if (selectedEntry == 2) selectedEntry = 0;
                else if (selectedEntry == 3) selectedEntry = 1;
                else if (selectedEntry == 4) selectedEntry = 2;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
            {
                if (selectedEntry == 0) selectedEntry = 2;
                else if (selectedEntry == 1) selectedEntry = 3;
                else if (selectedEntry == 2) selectedEntry = 4;
                else if (selectedEntry == 3) selectedEntry = 4;
                else if (selectedEntry == 4) selectedEntry = 0;
            }
        }

        if (direction == MenuEntryDirection.OPTIONSCREEN)
        {
            if (InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
            {
                if (selectedEntry == 0 && AudioManager.MusicVolume > 0.0f)
                {
                    if (AudioManager.MusicVolume < 0.1f)
                        AudioManager.MusicVolume = 0.0f;
                    else
                        AudioManager.MusicVolume -= 0.1f;
                }
                else
                if (selectedEntry == 1 && AudioManager.SoundVolume > 0.0f)
                {
                    if (AudioManager.SoundVolume < 0.1f)
                        AudioManager.SoundVolume = 0.0f;
                    else
                        AudioManager.SoundVolume -= 0.1f;
                    AudioManager.PlaySound(okSound);
                }
                else
                if (selectedEntry == 2) selectedEntry = 3;
                else if (selectedEntry == 3) selectedEntry = 2;
                else if (selectedEntry == 4) selectedEntry = 2;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorRight))
            {
                if (selectedEntry == 0 && AudioManager.MusicVolume < 1.0f)
                {
                    if (AudioManager.MusicVolume > 0.9f)
                        AudioManager.MusicVolume = 1.0f;
                    else
                        AudioManager.MusicVolume += 0.1f;
                }
                else
                if (selectedEntry == 1 && AudioManager.SoundVolume < 1.0f)
                {
                    if (AudioManager.SoundVolume > 0.9f)
                        AudioManager.SoundVolume = 1.0f;
                    else
                        AudioManager.SoundVolume += 0.1f;

                    AudioManager.PlaySound(okSound);
                }
                else
                if (selectedEntry == 2) selectedEntry = 3;
                else if (selectedEntry == 3) selectedEntry = 2;
                else if (selectedEntry == 4) selectedEntry = 3;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
            {
                if (selectedEntry == 0) selectedEntry = 4;
                else if (selectedEntry == 1) selectedEntry = 0;
                else if (selectedEntry == 2) selectedEntry = 1;
                else if (selectedEntry == 3) selectedEntry = 1;
                else if (selectedEntry == 4) selectedEntry = 2;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
            {
                if (selectedEntry == 0) selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 2;
                else if (selectedEntry == 2) selectedEntry = 4;
                else if (selectedEntry == 3) selectedEntry = 4;
                else if (selectedEntry == 4) selectedEntry = 0;
            }
        }

        if (direction == MenuEntryDirection.PAUSESCREEN)
        {
            if (InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
            {
                if (selectedEntry == 0) selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 0;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorRight))
            {
                if (selectedEntry == 0) selectedEntry = 1;
                else if (selectedEntry == 1) selectedEntry = 0;
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
            {
                if (selectedEntry == 0) selectedEntry = 2;
                else if (selectedEntry == 1) selectedEntry = 2;
                else if (selectedEntry == 2) selectedEntry = 0; 
            }

            if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
            {
                if (selectedEntry == 0) selectedEntry = 2;
                else if (selectedEntry == 1) selectedEntry = 2;
                else if (selectedEntry == 2) selectedEntry = 0; 
            }
        }

        // Accept or cancel the menu?
        if (InputManager.IsActionTriggered(InputManager.Action.Ok))
        {
            OnSelectEntry(selectedEntry);
        }
        else if (InputManager.IsActionTriggered(InputManager.Action.Back) ||
            InputManager.IsActionTriggered(InputManager.Action.ExitGame))
        {
            //Globals.IsPaused = false;
            OnCancel();
        }
        else if (selectedEntry != oldSelectedEntry)
        {
            AudioManager.PlaySound(selectedSound);
        }
    }

    protected virtual void OnSelectEntry(int entryIndex)
    {
        if (menuEntries.Count != 0)
        {
            menuEntries[selectedEntry].OnSelectEntry(okSound); 
        }
    }

    protected virtual void OnCancel()
    {
        ExitScreen();
    }

    protected void OnCancel(object sender, EventArgs e)
    {
        OnCancel();
    }

    public override void LoadContent()
    {
        //base.LoadContent();

    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                   bool coveredByOtherScreen)
    {
        if(IsActive)
            AudioManager.PlayMusic();

        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        // Update each nested MenuEntry object.
        for (int i = 0; i < menuEntries.Count; i++)
        {
            bool isSelected = IsActive && (i == selectedEntry);

            menuEntries[i].Update(this, isSelected, gameTime);
        }
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        // Draw each menu entry in turn.
        for (int i = 0; i < menuEntries.Count; i++)
        {
            MenuEntry menuEntry = menuEntries[i];

            bool isSelected = IsActive && (i == selectedEntry);

            menuEntry.Draw(this, isSelected, gameTime);
        }

        spriteBatch.End();
    }

}


